package com.example.generic_.generic01;

import java.util.ArrayList;

public class Generic01 {
    public static void main(String[] args) {
        //传统方法添加arraylist
        ArrayList arrayList = new ArrayList();
        arrayList.add(new Dog("旺财", 10));
        arrayList.add(new Dog("发财", 1));
        arrayList.add(new Dog("小黄", 5));

        //程序员不小心添加了一个猫类
//        arrayList.add(new Cat("tom", 5));//可以运行,但是会报类型转换错误 ClassCastException

        //遍历
        for (Object o : arrayList) {
            Dog dog = (Dog) o;//向下转型
            System.out.println(dog.getName() + "-" + dog.getAge());
        }
    }
}
